package com.aaa.controller;

import com.aaa.entity.Student;
import com.aaa.service.StudentService;
import com.aaa.util.DefaultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/stu")
public class StudentController {
@Autowired
    StudentService studentService;
@RequestMapping("/selectAll")
    public List<Student> selectAll(){
    List<Student> list = studentService.list();
    return list;
}
@RequestMapping("saveOrUpdate")
    public DefaultMsg insert(Student student){
    DefaultMsg defaultMsg=new DefaultMsg();
    boolean save = studentService.saveOrUpdate(student);
    if (!save){
        defaultMsg.setCode(500);
        defaultMsg.setSuccess(0);
        defaultMsg.setError("操作失败");
    }
    return defaultMsg;
}
@RequestMapping("/delete")
    public DefaultMsg delete(Student student){
    DefaultMsg defaultMsg=new DefaultMsg();
    boolean b = studentService.removeById(student.getId());
    if (!b){
        defaultMsg.setCode(500);
        defaultMsg.setSuccess(0);
        defaultMsg.setError("操作失败");
    }
    return defaultMsg;
}
}
